<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table
              :data="roles"
              style="width: 100%"
            >
              <el-table-column label="序号" type="index" :index="orderIndex" />
              <el-table-column label="角色名称" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="mini" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
                  <el-button size="mini" type="primary" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="hDelete(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- :current-page 设置当前的页码
              :page-sizes 可供选中的每页的条数
              :page-size 当前每页的条数''
              layout 决定布局元素的顺序
              total 一共有的多少条数据

               @size-change : 页码变换时
                @current-change: @size-change 每天显示多少条,变化时触发
             -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane></el-tabs>
      </el-card>
    </div>

    <!-- 新增弹框 -->
    <el-dialog
      :title="isEdit ? '编辑角色' : '新增角色'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="hClose"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>

    <!-- 分配权限 -->
    <!-- 分配权限的弹层 -->
    <el-dialog
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible.sync="showDialogAssign"
    >
      <assign-permission v-if="showDialogAssign" :role-id="roleId" :show-dialog-assign.sync="showDialogAssign" />
    </el-dialog>
  </div>
</template>

<script>
import { getRoles, deleteRole, addRoles, updateRole } from '@/api/settings'
import assignPermission from './assignPermission.vue'
export default {
  components: { assignPermission },
  data() {
    return {
      roleId: '',
      showDialogAssign: false,
      isEdit: false,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      roles: [],
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 5 // 每页5条  --- 要与pagination中page-size一致
      },
      total: 0 // 总的数据条数
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 用户点击分配权限
    hAssign(id) {
      this.showDialogAssign = true
      this.roleId = id
    },
    async doEdit() {
      await updateRole(this.roleForm)
      this.$message.success('编辑成功')
      this.showDialog = false

      this.loadRoles()
    },
    // 点击编辑按钮
    hEdit(row) {
      this.showDialog = true
      this.isEdit = true
      this.roleForm = { ...row }
      // console.log(row, 99)
    },
    // 点击新增按钮
    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    async doAdd() {
      await addRoles(this.roleForm)
      this.$message.success('添加成功')
      this.showDialog = false

      this.total++
      this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize)
      this.loadRoles()
    },
    // 点击弹窗确认按钮
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    hClose() {
      // 表单数据重置
      this.$refs.roleForm.resetFields()

      this.roleForm.id = ''
      this.roleForm.name = ''
      this.roleForm.description = ''
    },
    // 点击删除按钮
    hDelete(id) {
      this.$confirm('确认删除部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    async doDel(id) {
      await deleteRole(id)
      this.$message.success('删除成功')
      if (this.roles.length === 1) {
        this.pageParams.page--
        if (this.pageParams.page <= 0) {
          this.pageParams.page = 1
        }
      }
      this.loadRoles()
    },
    async loadRoles() {
      const res = await getRoles(this.pageParams)
      console.log(res)
      this.roles = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.pageParams.pagesize = val
      this.loadRoles()
    },
    // 点击页码
    handleCurrentChange(val) {
      // 1. 更新页码
      this.pageParams.page = val
      // 2. 重发请求
      this.loadRoles()
    },
    // 优化分页
    orderIndex(e) {
      // console.log(e, 3)

      return e + 1 + (this.pageParams.page - 1) * this.pageParams.pagesize
    }
  }
}
</script>
